package com.dayatang.android.Activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dayatang.android.Activity.Application.MyApplication;
import com.dayatang.android.Model.MyTask;
import com.dayatang.android.Task.BaseTask;
import com.dayatang.android.Task.LoadMyTaskTask;
import com.dayatang.android.Adapter.MyTaskListViewAdapter;

public class MyTaskActivity extends FootCommonActivity {
	
	private ListView listView;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		AlabSetContentView(R.layout.mytask_activity);
		AlabSetBarTitleText(getString(R.string.task_mytask));
		 
		UpdateMyTaskNum();
		
		InitListView();
		
	}
	
	private void UpdateMyTaskNum(){
		
		String url = getServerAddress() + "/remote/load-mytask-num.action?userid="+getUser().getId();
		
		try {
			String result = new BaseTask(this).execute(url).get();
			saveTaskNum(result);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
	}
	
	private void saveTaskNum(String result) {
		MyApplication application = (MyApplication) this.getApplication();
		application.setTaskNum(result);
	}
	
	private void InitListView(){
		
		listView = (ListView) findViewById(R.id.mytask_listview);
		
		List<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
		
		try {
			String result = new LoadMyTaskTask(this).execute(getServerAddress(),String.valueOf(getUser().getId())).get();
			
			if(result.equals("error")){
				Toast.makeText(this,getString(R.string.serverAddress_alert_addressError), Toast.LENGTH_LONG).show();  
			}else if(result.equals("NoData")){
				TextView textView = (TextView) findViewById(R.id.mytask_alert_testView);
				textView.setVisibility(View.VISIBLE);
			}else if(result != null){
				
				JSONArray jsonArray = new JSONArray(result);
				
				for(int i=0;i<jsonArray.length();i++){
					
					JSONObject jsonObject =(JSONObject) jsonArray.get(i);
					
					MyTask task = new MyTask();
					task.setSubProjectId(jsonObject.getString("subProjectId"));
					task.setProjName(jsonObject.getString("projName"));
					task.setResponsibleName(jsonObject.getString("responsibleName"));
					task.setWorkStepName(jsonObject.getString("workStepName"));
					task.setProgressStatus(jsonObject.getString("progressStatus"));
					task.setSubProjectName(jsonObject.getString("subprojName"));
					
				    HashMap<String, Object> map = new HashMap<String, Object>();
				    map.put("TaskNum", task.getProjName());
					map.put("Task", task.getWorkStepName());
					map.put("SubprojectId", task.getSubProjectId());
				    listItem.add(map); 
					
				}
			
			}
			listView.setAdapter(new MyTaskListViewAdapter(MyTaskActivity.this, listItem,getServerAddress()));

		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}catch (JSONException e) {
			e.printStackTrace();
			Toast.makeText(this, "数据出错", 2000).show();
		}
		
	}
	
}
